package be.jaisy.osgi.transaction;
/**
 * A simple {@link org.springframework.transaction.TransactionStatus} implementation.
 * 
 * @author Stany Blanvalet
 * @see org.springframework.transaction.support.TransactionStatus
 */
public class TransactionStatus {

	private boolean rollbackOnly = false;

	private boolean completed = false;
	
	private final boolean newTransaction;


	/**
	 * Creates a new instance of the {@link TransactionStatus} class,
	 * indicating a new transaction.
	 */
	public TransactionStatus() {
		this(true);
	}

	/**
	 * Creates a new instance of the {@link TransactionStatus} class.
	 * @param newTransaction whether to indicate a new transaction
	 */
	public TransactionStatus(boolean newTransaction) {
		this.newTransaction = newTransaction;
	}


	public boolean isNewTransaction() {
		return newTransaction;
	}
	
	public void setRollbackOnly() {
		this.rollbackOnly = true;
	}

	/**
	 * Determine the rollback-only flag via checking both the local rollback-only flag
	 * of this TransactionStatus and the global rollback-only flag of the underlying
	 * transaction, if any.
	 * @see #isLocalRollbackOnly()
	 * @see #isGlobalRollbackOnly()
	 */
	public boolean isRollbackOnly() {
		return (isLocalRollbackOnly() || isGlobalRollbackOnly());
	}

	/**
	 * Determine the rollback-only flag via checking this TransactionStatus.
	 * <p>Will only return "true" if the application called <code>setRollbackOnly</code>
	 * on this TransactionStatus object.
	 */
	public boolean isLocalRollbackOnly() {
		return this.rollbackOnly;
	}

	/**
	 * Template method for determining the global rollback-only flag of the
	 * underlying transaction, if any.
	 * <p>This implementation always returns <code>false</code>.
	 */
	public boolean isGlobalRollbackOnly() {
		return false;
	}

	/**
	 * Mark this transaction as completed, that is, committed or rolled back.
	 */
	public void setCompleted() {
		this.completed = true;
	}

	public boolean isCompleted() {
		return this.completed;
	}

}